package com.example.homelicaisystem.services.impl;

import com.example.homelicaisystem.dao.IncomeDetailMapper;
import com.example.homelicaisystem.dao.UserMapper;
import com.example.homelicaisystem.entity.*;
import com.example.homelicaisystem.entity.DTO.FamilyDTO;
import com.example.homelicaisystem.entity.DTO.IncomeDetailDTO;
import com.example.homelicaisystem.services.FamilyService;
import com.example.homelicaisystem.services.IncomeDetailService;
import com.example.homelicaisystem.utils.UserUtil;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
public class IncomeDetailImpl implements IncomeDetailService {
    private final IncomeDetailMapper incomeDetailMapper;
    private final FamilyService familyService;

    public IncomeDetailImpl(IncomeDetailMapper incomeDetailMapper, UserMapper userMapper, FamilyService familyService) {
        this.incomeDetailMapper = incomeDetailMapper;

        this.familyService = familyService;
    }


    @Override
    public Page<IncomeDetail> getList(IncomeDetailDTO data) {

        PageHelper.startPage(data.getPageNum(), data.getPageSize());
        return incomeDetailMapper.getList(data);
    }


    @Override
    public void delete(int id) {
        incomeDetailMapper.delete(id);
    }

    @Override
    public void modify(IncomeDetail data) {
        incomeDetailMapper.update(data);
    }

    @Override
    public IncomeDetail create(IncomeDetail data) {
        //记录当前时间
        data.setDate(new Date());
        //token获取id
        int userId = UserUtil.getUserId();
        data.setUserId(userId);
        //插入收入明细表
        incomeDetailMapper.insertIncomeDetail(data);
        return data;
    }

    @Override
    public List<SelectResult> selectIncomeType() {
        return incomeDetailMapper.selectIncomeType();
    }

    //收入方式选择列表
    @Override
    public List<SelectResult> selectIncomeMode() {
        return incomeDetailMapper.selectIncomeMode();
    }

    @Override
    public IncomeDetail getIncomeDetailById(int id) {
        return incomeDetailMapper.getIncomeDetailById(id);
    }

    @Override
    public List<IncomeDetail> censusByMode(FamilyDTO data) {

        Integer familyId = familyService.getFamilyIdByUserId(UserUtil.getUserId());
        data.setId(familyId);
        return incomeDetailMapper.censusByMode(data);
    }


}
